package com.normalsys.live.base.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.normalsys.live.base.config.ResponseCode;
import com.normalsys.live.base.util.token.TokenUtil;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * Project Name: yuntai
 * File Name: ProgramInterceptor
 * Package Name: com.normalsys.manage.base.interceptor
 * Date: 2018/7/18  18:00
 * Copyright (c) 2018, tianyul All Rights Reserved.
 */
public class ProgramInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object o) throws Exception {
        //校验登录信息
        if(TokenUtil.isOverDue(req)) {
            Map<String,Object> result = new HashMap<>();
            result.put("code", ResponseCode.TOKEN_OVERDUE);
            result.put("msg","登录信息失效，请重新登录");

            resp.setContentType("text/html;charset=UTF-8");

            PrintWriter writer = resp.getWriter();
            writer.write(JSONObject.toJSONString(result));
            writer.flush();
            writer.close();
            return false;
        }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
